
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>  
#include <direct.h> 
#include <vector>
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/fstream.hpp"

#include "../txr_algo_dlm_seg_v1/common/define.h"
#include "../txr_algo_dlm_seg_v1/common/gpu_utility.h"
#include "yolo_v5.h"

std::vector<std::string> EnumFiles(std::string path, std::string extension)
{
	namespace fs = boost::filesystem;
	fs::path _path(path);
	fs::path fullpath = fs::system_complete(_path);

	std::vector<std::string> ret;

	if (!fs::exists(fullpath) && !fs::is_directory(fullpath))
	{
		return ret;
	}
	fs::directory_iterator end_iter;
	for (fs::directory_iterator iter(fullpath); iter != end_iter; iter++)
	{
		try {
			if (fs::is_directory(*iter))
			{
				//ret.push_back(iter->path().wstring());
			}
			else if (iter->path().extension() == extension)
			{
				ret.push_back(iter->path().string());
			}
		}
		catch (const std::exception&)
		{
			continue;
		}
	}
	return ret;
}

void enum_models(std::string& folder)
{
	std::string gpu_model = GetGPUModel();
	if (gpu_model == "")
	{
		std::cout << "Unknown Gpu Model! \n";
		return;
	}

	std::vector<std::string> cfg_files = EnumFiles(folder, std::string(".yaml"));

	for (int i = 0; i < cfg_files.size(); ++i)
	{
		namespace fs = boost::filesystem;
		fs::path _cfg_path(cfg_files[i]);
		fs::path _cfg_stem = _cfg_path.stem();

		std::cout << "START===============================================\n";
		st_trans_model_input input;
		input.cfg_path = cfg_files[i];
		input.gpu_model = gpu_model;
		
		yolo_v5 c;
		if (c.TransModel(input))
		{
			std::cout << "DONE================================================\n";
		}
		else
		{
			std::cout << "FAIL!================================================\n";
		}
	}
}

int main()
{
	char cur_folder[260];
	_getcwd(cur_folder, sizeof(cur_folder));
	printf("current working directory: \n %s\n", cur_folder);
	std::string cwd = cur_folder;

	enum_models(cwd);

	getchar();
}
